<template>
  <view>
    <view class="scroll-container">
      <!-- 左侧滚动区域 -->
      <scroll-view class="left-scroll-list" scroll-y="true" :style="{height: window_height+'px'}">
        <block v-for="(item,i) in cateList" :key="i">
          <view class="left-item" :class="[active===i?'active':'']" @click="changeActive(i)">{{item.cat_name}}</view>
        </block>
      </scroll-view>
      <!-- 右侧滚动区域  -->
      <scroll-view class="right-scroll-list" scroll-y="true" :style="{height: window_height+'px'}"
        :scroll-top="scrollTop">
        <view class="right-item" v-for="(item,i) in cateLevel2" :key="i">
          <view class="right-item">{{item.cat_name}}</view>
          <!-- 三级分类列表 -->
          <view class="cate3_list">
            <view class="cate3" v-for="(item2,i2) in item.children" :key="i2" @click="gotoGoodsList(item2)">
              <image :src="item2.cat_icon.replace('dev','web')" mode="widthFix"></image>
              <text>{{item2.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        window_height: 0,
        cateList: [],
        cateLevel2: [],
        active: 0,
        scrollTop: 0
      };
    },
    onLoad() {
      // 获取不同设备的可使用的窗口高度
      const sysInfo = uni.getSystemInfoSync()
      this.window_height = sysInfo.windowHeight

      this.getCateList()
    },
    methods: {
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/categories')
        if (res.meta.status !== 200) return uni.$showMsg()
        this.cateList = res.message
        this.cateLevel2 = res.message[0].children
      },
      // 点击添加类active
      changeActive(i) {
        this.active = i
        this.cateLevel2 = this.cateList[i].children
        this.scrollTop === 0 ? this.scrollTop = 1 : this.scrollTop = 0
      },
      gotoGoodsList(item2) {
        uni.navigateTo({
          url: `/subpck/goods_list/goods_list?cid=${item2.cat_id}`
        })
      }
    }
  }
</script>

<style lang="scss">
  .scroll-container {
    display: flex;

    .left-scroll-list {
      width: 120px;

      .left-item {
        line-height: 60px;
        text-align: center;
        background-color: #f7f7f7;
        font-size: 12px;

        &.active {
          background-color: #efefef;
          position: relative;

          &::before {
            content: "";
            display: block;
            height: 30px;
            width: 3px;
            background-color: #C00000;
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
          }
        }
      }
    }

    .right-scroll-list {
      .right-item {
        text-align: center;
        font-size: 12px;
        font-weight: bold;
        padding: 15px 0;
      }

      .cate3_list {
        display: flex;
        flex-wrap: wrap;

        .cate3 {
          width: 33.33%;
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          margin-bottom: 15px;

          image {
            width: 60px;
            height: 60px;
          }

          text {
            font-size: 12px;
          }
        }
      }
    }
  }
</style>
